﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant.User_Control
{
    public partial class ThongKeNV : UserControl
    {
        private NhanVienService nvService = new NhanVienService();
        private NhaHangService nhService = new NhaHangService();
        public bool fix = false;
        public bool unrepeat = false;
        public bool phantom = false;

        private int soluong = 0;
        private int nam = 0;
        private int nu = 0;

        public ThongKeNV()
        {
            InitializeComponent();
        }

        private void HienThiThongTin(DataTable dbtab)
        {
            lbSoLuong.Text = soluong.ToString();
            lbNam.Text = nam.ToString();
            lbNu.Text = nu.ToString();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                liv.SubItems.Add(dbrow[4].ToString());
                liv.SubItems.Add(dbrow[5].ToString());
                liv.SubItems.Add(nvService.LayLoaiNV(int.Parse(dbrow[6].ToString())));
                liv.SubItems.Add(nhService.LayTenNH(int.Parse(dbrow[7].ToString())));
                liv.SubItems.Add(dbrow[9].ToString());
                lv_NV.Items.Add(liv);
            }
        }

        private void LoadNV()
        {
            lv_NV.Items.Clear();
            DataTable dbtab = new DataTable();
            if (unrepeat && !fix)
            {
                dbtab = nvService.LoadNVUnrepeatError(ref soluong,ref nam,ref nu);
                HienThiThongTin(dbtab);
                return;
            }
            if (unrepeat && fix)
            {
                dbtab = nvService.LoadNVUnrepeat(ref soluong, ref nam, ref nu);
                HienThiThongTin(dbtab);
                return;
            }
            if (phantom && !fix)
            {
                dbtab = nvService.LoadNVPhantomError(ref soluong, ref nam, ref nu);
                HienThiThongTin(dbtab);
                return;
            }
            if (phantom && fix)
            {
                dbtab = nvService.LoadNVPhantom(ref soluong, ref nam, ref nu);
                HienThiThongTin(dbtab);
                return;
            }
            dbtab = nvService.LayTatCaNV();
            HienThiThongTin(dbtab);
        }

        private void ThongKeNV_Load(object sender, EventArgs e)
        {
            LoadNV();
        }

        private void btnTaiKhoan_Click(object sender, EventArgs e)
        {
            if (lv_NV.FocusedItem.Index >= 0)
            {
                int index = lv_NV.FocusedItem.Index;
                int manv = int.Parse(lv_NV.Items[index].Text);
                NhanVien nv = nvService.TimNVID(manv);
                if (nv.Username == "")
                {
                    frmTaoTaiKhoan frm = new frmTaoTaiKhoan();
                    frm.nv = nv;
                    frm.ShowDialog();
                    if (frm.ok)
                    {
                        
                        LoadNV();
                    }
                    else
                    { return; }
                }
            }
            else
            {
                return;
            }
        }
    }
}
